﻿using System.Collections.Generic;

namespace Pisces
{
	/**
	 * 转换关系
     * created by wangqiang
    **/
	public class FSMTransitionRelation<S>
	{
		
		/**
         * 转换关系对应map
         * key:源状态, value:目标状态
         */
		private Dictionary<S, HashSet<S>> relationMap = new Dictionary<S, HashSet<S>>();

		/**
		 * 增加转换关系
		 * @param source  源状态
		 * @param destination 目标状态
		 * @return
		 */
		public void addTransition(S source, S destination)
		{
			HashSet<S> states = relationMap.GetValueOrDefault(source);
			if (states == null)
			{
				states = new HashSet<S>();
				relationMap.Add(source, states);
			}

			states.Add(destination);
		}

		/**
		 * 移除转换关系
		 * @param source 源状态
		 * @param destination 目标状态
		 */
		public void removeTransition(S source, S destination)
		{
			HashSet<S> states = relationMap.GetValueOrDefault(source);
			if (states != null)
			{
				states.Remove(destination);
			}
		}

		/**
		 * 是否可以转换到目标状态
		 * @param source
		 * @param destination
		 * @return
		 */
		public bool canTransitionTo(S source, S destination)
		{
			HashSet<S> states = relationMap.GetValueOrDefault(source);
			if (states == null)
			{
				return false;
			}

			return states.Contains(destination);
		}
	}
}